<template>
    <div class="admin-page">
        <div class="page-header">
            <el-tree
                :data="treeData"
                :props="defaultProps"
                default-expand-all
                node-key="id"
                style="width: 100%;height: 65vh;overflow-y: auto;margin-top: 20px;"
            >
                <span class="custom-tree-node" slot-scope="{ node, data }">
                    <span>{{ data.id }} --- {{ data.username }} --- {{ data.nickname }} (业绩: {{ data.money }}) --- {{
                            getLevelName(data.is_up)
                        }}</span>
                </span>
            </el-tree>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            treeData: [],
            defaultProps: {
                children: 'children',
                label: 'username'
            },
            form: {
                search: "",
                state: "",
                start_time: "", // 开始时间
                end_time: "" // 结束时间
            },
            showDialog: false,
            showDialog1: false,
            id: null,
            type: "",
            levelMap: [
                "萌新",
                "初级服务商",
                "中级服务商",
                "高级服务商",
                "区域服务商",
                "城市服务商",
                "大区服务商",
                "全球服务商"
            ]
        };
    },
    mounted() {
        this.id = this.$route.query.id;
        this.type = this.$route.query.type;
        this.fetchAdminList();
    },
    methods: {
        getLevelName(value) {
            const index = parseInt(value);
            return this.levelMap[index] || "未知等级";
        },
        fetchAdminList() {
            const params = {
                id: this.id, type: this.type
            }
            this.$http.get('/admin/User/getUserTree', {params}).then(result => {
                console.log(result)
                if (result.code === 200) {
                    // 构建树形结构数据
                    const dataArray = Object.values(result.data);
                    if (dataArray.length > 0) {
                        // 第一条数据作为根节点
                        const rootNode = dataArray[0];
                        rootNode.children = [];

                        // 从第二条数据开始，每条数据都是前一条的子节点
                        for (let i = 1; i < dataArray.length; i++) {
                            const currentNode = dataArray[i];
                            const parentNode = dataArray[i - 1];

                            currentNode.children = [];
                            if (parentNode.children) {
                                parentNode.children.push(currentNode);
                            }
                        }

                        this.treeData = [rootNode];
                    } else {
                        this.treeData = [];
                    }
                } else {
                    this.$message.error(result.msg);
                }
            });
        },
    }
};
</script>
<style lang="less" scoped>
.admin-page {
    padding: 24px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);

    .page-header {
        margin-bottom: 24px;
        padding-bottom: 16px;
        border-bottom: 1px solid #f0f2f5;
    }
}
</style>



